<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News extends CI_Model {
    public function __construct() {
    	parent::__construct();
    }
	
    public function getNews($options = array()) {
    	$options = _default(array('sortDirection' => 'desc', 'sortBy' => 'new_date'), $options);
    	$queryFields = array('new_id', 'new_type', 'new_objid', 
                             'new_title', 'new_date', 'new_route');

    	foreach ($queryFields as $field) {
    		if (isset($options[$field]))
    			$this->db->where($field, $options[$field]);
    	}
    	
    	if (isset($options['limit']) && isset($options['offset']))
    		$this->db->limit($options['limit'], $options['offset']);
    	else if (isset($options['limit']))
    		$this->db->limit($options['limit']);
    	
    	if (isset($options['sortBy']))
    		$this->db->order_by($options['sortBy'], $options['sortDirection']);
    	$query = $this->db->get('acs_news');
    	if ($query->num_rows() == 0)
    		return FALSE;
    	if (isset($options['new_id']))
    		return $query->row_array(0);
    	else
    		return $query->result_array();
    }
    
    public function getLast10($options = array())
    {
        $opts = _default(array('sortBy' => 'new_date', 'sortDirection' => 'desc', 'limit' => 10), $options);
        return $this->getNews($opts);
    }
    
    public function updateNews($options = array()) {
		return false;
    }
    
    public function insertNews($options = array()) {
        $options = _default(array('new_date' => date('Y-m-d H:i:s')), $options);
    	$queryFields = array('new_type', 'new_objid', 
                             'new_title', 'new_route');

        if (!_required($queryFields, $options))
            return FALSE;
    	foreach ($queryFields as $field) {
    		if (isset($options[$field]))
    			$this->db->set($field, $options[$field]);
    	}
    	
    	$this->db->insert('acs_news');
    	return $this->db->insert_id();
    }
    
	public function deleteNews($options = array()) {
		return false;
    }
}
